#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- 
# --
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2021/7/7 11:56
# Email lmay@lmaye.com
# ----------------------------------------------------------
import os
import threading
import time
from datetime import datetime

import pandas

from core import LOG
from core.constant.sys_enum import SysEnum


def thread_list(load_path, out_path, take_file, source_columns, where_columns, add_columns, signal_out=None):
    threads = []
    for file in os.listdir(load_path):
        thread = threading.Thread(target=handle_file, args=(load_path + SysEnum.SEPARATOR.value + file, out_path,
                                                            take_file, source_columns, where_columns, add_columns,
                                                            signal_out))
        threads.append(thread)
    return threads


def handle_file(excel_file, out_path, take_file, source_columns, where_columns, add_columns, signal_out=None):
    try:
        thread_name = threading.currentThread().name
        basename = os.path.basename(excel_file)
        LOG.info("任务开始[{}]读取文件: {}".format(thread_name, basename))
        if signal_out:
            signal_out.emit("{} - 线程 [{}] 读取文件: {}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), thread_name,
                                                           basename))
        start = time.time()
        take_data = None
        # 获取取数表数据
        if take_file:
            take_data = pandas.read_excel(take_file, engine="openpyxl", sheet_name=0,
                                          usecols=list(add_columns).append(where_columns))
            LOG.info("取数表-数据: \n{}".format(take_data))
        # 获取元数据
        records = pandas.read_excel(excel_file, engine="openpyxl", sheet_name=0, usecols=source_columns)
        end = time.time()
        time_consuming = end - start
        LOG.info("读取数据结束[{}] {}".format(thread_name, excel_file))
        LOG.info("读取耗时[{}]: {}".format(thread_name, time_consuming))
        if signal_out:
            signal_out.emit("{} - 线程 [{}] 读取 [{}] 耗时: {:.2f}s".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                                                                      thread_name, basename, time_consuming))
        # 合并数据
        rec_merge = pandas.DataFrame(records)
        if take_file:
            rec_merge = pandas.merge(left=records, right=take_data, left_on=where_columns, right_on=where_columns,
                                     how="left")
            new_columns = rec_merge.columns.to_list()
            # 按逆序insert
            for name in add_columns[::-1]:
                new_columns.remove(name)
                new_columns.insert(new_columns.index("记账本位币币别") + 1, name)
            rec_merge = rec_merge.reindex(columns=new_columns)
        # 获取输出文件之前内容
        write_name = str(basename).replace(".xlsx", "") + ".csv"
        rec_merge.to_csv(out_path + SysEnum.SEPARATOR.value + write_name, index=False, encoding="utf_8_sig")
        time_consuming2 = time.time() - end
        LOG.info("写入数据结束[{}]".format(thread_name))
        LOG.info("写入耗时[{}]: {}".format(thread_name, time_consuming2))
        if signal_out:
            signal_out.emit("{} - 线程 [{}] 写入 [{}] 耗时: {:.2f}s".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                                                                      thread_name, write_name, time_consuming2))
        LOG.info("-------------------------------------------------\n")
    except Exception as e:
        LOG.error("{} 文件处理失败... {}".format(basename, e))
        if signal_out:
            signal_out.emit("{} - 文件处理失败: {}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), e))


def merge_files(out_path, cr_type, signal_out=None):
    try:
        thread_name = threading.currentThread().name
        LOG.info("文件合并开始[{}]".format(thread_name))
        if signal_out:
            signal_out.emit("{} - 线程 [{}] 文件合并中...".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), thread_name))
        start = time.time()
        if "csv" == cr_type:
            files = [out_path + SysEnum.SEPARATOR.value + file for file in os.listdir(out_path) if file.endswith('.csv')]
            pc = pandas.concat(map(pandas.read_csv, files))
            pc.to_csv(out_path + SysEnum.SEPARATOR.value + "output.csv", index=False, encoding="utf_8_sig")
        else:
            excels = []
            for file in os.listdir(out_path):
                excels.append(pandas.read_excel(out_path + SysEnum.SEPARATOR.value + file))
            writer = pandas.ExcelWriter(out_path + SysEnum.SEPARATOR.value + "output.xlsx")
            pandas.concat(excels).to_excel(writer, index=False)
            writer.save()
        time_consuming = time.time() - start
        LOG.info("文件合并结束[{}]".format(thread_name))
        LOG.info("合并耗时[{}]: {}".format(thread_name, time_consuming))
        if signal_out:
            signal_out.emit("{} - 线程 [{}] 合并耗时: {:.2f}s".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                                                                thread_name, time_consuming))
    except Exception as e:
        LOG.error("文件合并失败...", e)
        if signal_out:
            signal_out.emit("{} - 文件合并失败: {}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), e))
